package com.inspectortime.webapp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.inspectortime.domain.Inspection;
import com.inspectortime.domain.type.InspectionStatus;
import com.inspectortime.repository.Repository;

public class SchedulingCancelController implements Controller {

	private String successView;
	private Repository repository;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String inspectionId = request.getParameter("inspectionId");
		Inspection inspection = repository.loadInspection(inspectionId);
		InspectionStatus status = inspection.getInspectionStatus();
		inspection.setInspectionStatus(InspectionStatus.CANCELLED);
		status = inspection.getInspectionStatus();
		inspection.save();		
		ModelAndView mv = new ModelAndView(successView);
		mv.addObject("message", "Inspection was cancelled.");
		return mv;
	}

	public void setSuccessView(String formView) {
		this.successView = formView;
	}
	
	public void setRepository(Repository repository) {
		this.repository = repository;
	}

}
